package com.tom.demo.common.component;

import cn.dev33.satoken.secure.SaSecureUtil;
import org.springframework.stereotype.Component;
import org.springframework.util.StringUtils;

/**
 * 对称密码加密
 */
@Component
public class PwdEncryption {
    // 定义秘钥和明文
    String secretKey = "lqabcdegflll";

    /**
     * 加密，参数是明文密码
     * @param password
     * @return
     */
    public String aesEncrypt(String password){
        return SaSecureUtil.aesEncrypt(secretKey,password);
    }

    /**
     * 解密，参数是密文密码
     * @param ciphertext
     * @return
     */
    public String aesDecrypt(String ciphertext){
        return SaSecureUtil.aesDecrypt(secretKey,ciphertext);
    }

    public boolean decrypt(String password,String ciphertext){
        if(StringUtils.hasText(password) && StringUtils.hasText(ciphertext)){
            return password.equals(this.aesDecrypt(ciphertext));
        }
        return false;
    }
}
